<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    

<script>
// Promise的静态方法
// Promise.race()竞速模式,比较多个promise对象谁最先完成
// race方法接受一个数组作为参数,数组中通常是多个promise对象
// race方法执行后返回一个新的promise对象

let p1 = new Promise((resolve,reject)=>{
    setTimeout(()=>{
        console.log( '任务1' );
        resolve('成功1');
    },1234)
})

let p2 = new Promise((resolve,reject)=>{
    setTimeout(()=>{
        console.log( '任务2' );
        resolve('成功2');
    },2234)
})

let p3 = new Promise((resolve,reject)=>{
    setTimeout(()=>{
        console.log( '任务3' );
        // resolve('成功3');
        reject('失败3');
    },834)
})

// p4的状态和结果:
// p4的状态和结果跟随最先完成的那个promise对象的状态和结果
let p4 = Promise.race([p1,p2,p3]);
p4.then(
    (res)=>{
        console.log( 'p4成功',res );
    },
    (res)=>{
        console.log( 'p4失败',res );
    }
)

</script>
</body>
</html>